//
//  HMEmoticonToolBar.swift
//  微博
//
//  Created by 程双岐 on 16/10/6.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

enum HMEmoticonToolBarButtonType: Int {
    case recent = 1000
    case normal = 1001
    case emoji = 1002
    case lxh = 1003
}




@available(iOS 9.0, *)
class HMEmoticonToolBar: UIStackView {

   var lastButton: UIButton?
   var callBack: ((HMEmoticonToolBarButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        axis = .horizontal
        distribution = .fillEqually
        
        addChildButton(title: "最近", imageName: "compose_emotion_table_left", type: .recent)
        addChildButton(title: "默认", imageName: "compose_emotion_table_mid", type: .normal)
        addChildButton(title: "Emoji", imageName: "compose_emotion_table_mid", type: .emoji)
        addChildButton(title: "浪小花", imageName: "compose_emotion_table_right", type: .lxh)
    }
    
    private func addChildButton(title: String, imageName: String, type: HMEmoticonToolBarButtonType) {
        let button = UIButton()
        button.tag = type.rawValue
        button.setTitle(title, for: .normal)
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), for: .selected)
        button.adjustsImageWhenHighlighted = false
        addArrangedSubview(button)
//        if type == .normal {
//            lastButton?.isHidden = false
//            button.isHidden = true
//            lastButton = button
//        }
    }
    
    @objc private func buttonAction(btn: UIButton){
        if lastButton == btn {
            return
        }
        lastButton?.isSelected = false
        btn.isSelected = true
        lastButton = btn
        let type = HMEmoticonToolBarButtonType(rawValue: btn.tag)
        callBack?(type!)
    }
    
    
    func seleccedButton(section: Int)  {
        let button = viewWithTag(section + 1000) as! UIButton
        
        if lastButton == button {
            return
        }
        
        
        lastButton?.isSelected = false
        button.isSelected = true
        lastButton = button
        print(section)
        
        
    }
}
